﻿using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading.Tasks;

namespace GitDC.SignalR
{
    public static class SendHubs
    {
        private static HubConnection hubConnection;

        /// <summary>
        /// 启动客户端
        /// </summary>
        public static async Task Start()
        {
            hubConnection = new HubConnectionBuilder()
                .WithUrl($"{SiteSetting.Current.Url}/chathub")
                .Build();

            await hubConnection.StartAsync();

            Console.WriteLine("连接状态" + hubConnection.State);
        }

        /// <summary>
        /// 调用hub方法
        /// </summary>
        /// <param name="methodName">方法名</param>
        public static async Task Send(string methodName)
        {
            if (hubConnection == null)
            {
                await Start();
            }

            if (hubConnection.State == HubConnectionState.Disconnected)
            {
                await Start();
            }

            await hubConnection.InvokeAsync(methodName, "a", "b");
        }
    }
}
